# -*- coding: cp936 -*-
import os
from optparse import OptionParser
par = OptionParser('')
par.add_option("-r", action="store_true", dest="run", default = False,help='run the script,copy mp3 to dstdir')
par.add_option("-p", action="store_true", dest="pause", default = True,help='pause the script when enter a dir')
par.add_option("-s","--srcdir",action = "store",type = "string",dest = "srcdir")
par.add_option("-d","--dstdir",action = "store",type = "string",dest = "dstdir")

(opt,args) = par.parse_args()

srcdir = opt.srcdir
dstdir = opt.dstdir
test = not opt.run
pause = opt.pause


print('####################',srcdir,dstdir,test,args,'###########')

idx = 0
tripstr = [' ','(Unabridged)','(Unabridge)','-']


lastdir = ''
for parent,dirs,files in os.walk(srcdir):
    for f in files:
        if f.lower().endswith('.mp3'):
            srcf = os.path.join(parent,f)
            idx = idx + 1
            name = '{:0>3}_'.format(idx)+f
            for empty in tripstr: name = name.replace(empty,'')
            dstf = os.path.join(dstdir,name)
            
            cmd = 'copy "{0}" "{1}"'.format(srcf,dstf)
            
            if test: 
                if lastdir != parent: 
                    if pause: input('continue:')
                    print(parent);
                    lastdir = parent;
                print('    ',f,'  ===>  ', name)
            
            if not test: 
                print(cmd)
                os.system(cmd)

